<?php
/*
 * dbmaster.php
 *
 * Copyright (C) 2006 - OS3 srl - http://www.os3.it
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

// DBMaster
//
// This is the one and only interface for all Database operations
class dbmaster
{
	var $prefix;	// Tables prefix (default: "wb")
	var $dbh;  	// Database Handler
	var $debug = 0;

	function dbmaster ()
	{
		$this->prefix = DB_PREFIX;

		$this->_open ();
		$this->rows = 0;
		$this->cols = 0;

		$this->reduce = true;
	}

	function & query ( $sql, $show_query = false )
	{
		if ( $show_query ) print ( "<h1>SQL: $sql</h1>" );

		error_log ( $sql );

		$stmt = $this->dbh->prepare ( $sql );
		$res = $stmt->execute ();

		if ( ! $res ) return $res;

		return $stmt;
	}

	function exec ( $sql )
	{
		error_log ( $sql );
		$stmt = $this->dbh->prepare ( $sql );
                $res = $stmt->execute ();

		return true;
	}

	function get_id ()
	{
		$row = $this->single_row ( "SELECT last_insert_id() AS lii" );
		return $row [ 'lii' ];
	}

	function lock ( $table_name )
	{
		$this->exec ( "LOCK TABLES " . $table_name . " WRITE");
	}

	function unlock ()
	{
		$this->exec ( "UNLOCK TABLES" );
	}

	function & single_row ( $sql, $show_query = false )
	{
		$res = $this->query ( $sql, $show_query );

		if ( $res == false ) return $res;

		$row = $res->fetch ();

		return $row;
	}

	function & fetch ( $stmt )
	{
		$row = $stmt->fetch ();

		return $row;
	}

	function & fetch_all ( $sql )
	{
		$r = $this->dbh->query ( $sql );
		if ( ! $r ) return $r;

		$res = $r->fetchAll ();
		return $res;
	}

	function free ( & $stmt )
	{
		// do nothing
		$stmt = null;
	}
	

	// This method open a pconnection to the database
	function _open ()
	{
		// Get right values for connection from the config class
		
		$db_url = DB_LOCATION; 
		$port	= DB_PORT;    
		$sock 	= DB_SOCKET; 

		if ( $port == "" )
		{
			if ( $sock != "" )	$db_url .= ":$sock";
		} else 
			$db_url .= ":$port";

		// Try a permanent connection
		$this->dbh = new PDO ( "mysql:host=" . DB_LOCATION . ";" .
				       "dbname=" . DB_NAME, DB_USER, DB_PASSWD ); // , array ( PDO::ATTR_PERSISTENT => true ) );
				       

		if ( $this->dbh == false )
		{
			$err  = "<table border=\"0\">";
			$err .= "<tr><td align=\"right\"><b>MySQL Err</b>:</td><td>" . mysql_error () . "</td></tr>";
			$err .= "<tr><td align=\"right\"><b>Location:</b></td><td>" . DB_LOCATION . "</td></tr>";
			$err .= "<tr><td align=\"right\"><b>Port:</b></td><td>" . $port . "</td></tr>";
			$err .= "<tr><td align=\"right\"><b>Socket:</b></td><td>" . $sock . "</td></tr>";
			$err .= "<tr><td align=\"right\"><b>User:</b></td><td>" . DB_USER . "</td></tr>";
			$err .= "</table>";

			util_display_error ( "Cannot establish a connection with MySQL", $err );

			return ( false );
		}

		return ( true );
	}

}
?>
